<?php



	require_once './controller.php';
	require_once '../connexion.php';
	
	class UserController extends Controller
	{
		
			public function __construct()
			{
				parent::__construct();
			}
			
			private function execute_login()
			{
				$doc = new Document();
				$doc->begin(0);
				$doc->contenu_login();
				$doc->end();
			}
			
			//lors d'un mauvais mot de passe ou adresse mail
			//on previens l'utilisateur
			private function execute_erreur(){
				$doc = new Document();
				$doc->begin(0);
				$doc->erreur_login();
				$doc->end();
			}
			
			private function execute_informations()
			{
				$doc = new Document();
				if(!isset($_SESSION['level'])){
					$doc->begin(0);
				}else if($_SESSION['level'] == '1' || $_SESSION['level'] == '2' || $_SESSION['level'] == '3'){
					$doc->begin($_SESSION['level']);
					$doc->informations();
					$doc->end();
				}
			
			}
			
			private function execute_modification_informations()
			{
				$doc = new Document();
				if(!isset($_SESSION['level'])){
					$doc->begin(0);
				}else if($_SESSION['level']=='1' || $_SESSION['level'] == '2' || $_SESSION['level'] == '3'){
					$doc->begin($_SESSION['level']);
					$doc->informationsModification();
					$doc->end();
				}
				
			}
			
			private function execute_index()
			{
				$doc = new Document();
				if(!isset($_SESSION['level'])){
					$doc->begin(0);
					$doc->contenu_accueil();
				}else{
					$doc->begin($_SESSION['level']);
					$doc->contenu_connected();
				}
				$doc->end();
			}
			
			private function execute_connexion()
			{
				$doc = new Document();
				$doc->begin($_SESSION['level']);
				$doc->contenu_connected();
				$doc->end();
			}
			
			/*return la page d'inscription */
			private function execute_inscription(){
				$doc = new Document();
				$doc->begin(0);
				$doc->contenu_inscription();
				$doc->end();
			}
			
			private function execute_cours(){
				
				if(!isset($_SESSION['level'])){
					$this->execute_erreur();
				}else if($_SESSION['level'] == '1'){
					$doc = new Document();
					$doc->begin($_SESSION['level']);
					$doc->contenu_cours();
					$doc->end();
				}
			
			}
			
			private function execute_liste_cours(){
				
				if(!isset($_SESSION['level'])){
					$this->execute_erreur();
				}else if($_SESSION['level'] == '1'){
					$doc = new Document();
					$doc->begin($_SESSION['level']);
					$doc->liste_cours();
					$doc->end();
				}
			
			}
			
			private function execute_inscription_cours(){
				
				if(!isset($_SESSION['level'])){
					$this->execute_erreur();
				}else if($_SESSION['level'] == '1'){
					$doc = new Document();
					$doc->begin($_SESSION['level']);
					$doc->inscription_cours();
					$doc->end();
				}
			
			}
			
			private function execute_inscrire_cours_adherent(){
				
				if(!isset($_SESSION['level'])){
					$this->execute_erreur();
				}else if($_SESSION['level'] == '1'){
					$doc = new Document();
					$doc->begin($_SESSION['level']);
					$doc->inscrireCoursByAdherent($_SESSION['id'] , $_POST['inscrire_cours']);
					$doc->end();
				}
			}
			
			private function execute_desinscrire_cours_adherent(){				
				
				if(!isset($_SESSION['level'])){
					$this->execute_erreur();
				}else if($_SESSION['level'] == '1'){
					$doc = new Document();
					$doc->begin($_SESSION['level']);
					$doc->desincrireCoursByAdherent($_SESSION['id'] , $_POST['desinscrire_cours']);
					$doc->contenu_cours();
					$doc->end();
				}
			}
			
			private function execute_planning(){
			$doc = new Document();
				if(!isset($_SESSION['level'])){
					$doc->begin(0);
				}else if($_SESSION['level'] == '2'){
					$doc->begin($_SESSION['level']);
					$doc->contenu_planning();
					$doc->end();
				}
			}
			private function execute_logout()
			{
				session_destroy();
				$doc = new Document();
				$doc->begin(0);
				$doc->contenu_accueil();
				$doc->end();
			}
			
			/***************Administration********************/
			
			private function execute_coursGestion_ajout(){
				$doc = new Document();
				
				
				if(!isset($_SESSION['level'])){
					$doc->begin(0);
				}else if($_SESSION['level'] == '3'){
					$doc->begin($_SESSION['level']);
					$doc->coursGestion_ajout();
					$doc->end();
				}
			}
			
			private function execute_coursGestion(){
				$doc = new Document();
				
				
				if(!isset($_SESSION['level'])){
					$doc->begin(0);
				}else if($_SESSION['level'] == '3'){
					$doc->begin($_SESSION['level']);
					$doc->coursGestion();
					$doc->end();
				}
			}
			
			private function execute_coursGestion_modif(){
				$doc = new Document();
				
				
				if(!isset($_SESSION['level'])){
					$doc->begin(0);
				}else if($_SESSION['level'] == '3'){
					$doc->begin($_SESSION['level']);
					$doc->coursGestion_modif();
					$doc->end();
				}
			}
			
			private function execute_professeurGestion(){
				$doc = new Document();				
				
				if(!isset($_SESSION['level'])){
					$doc->begin(0);
				}else if($_SESSION['level'] == '3'){
					$doc->begin($_SESSION['level']);
					$doc->ProfesseurGestion();
					$doc->end();
				}
			}	
			
			private function execute_professeurGestion_ajouter(){
				$doc = new Document();				
				
				if(!isset($_SESSION['level'])){
					$doc->begin(0);
				}else if($_SESSION['level'] == '3'){
					$doc->begin($_SESSION['level']);
					$doc->ProfesseurGestion_ajouter();
					$doc->end();
				}
			}
			
			private function execute_adherentGestion(){
				$doc = new Document();				
				
				if(!isset($_SESSION['level'])){
					$doc->begin(0);
				}else if($_SESSION['level'] == '3'){
					$doc->begin($_SESSION['level']);
					$doc->AdherentGestion();
					$doc->end();
				}
			}			
			
			protected function execute()
			{
				$action = $this->action;
				$dest = "";
				if($action == "index"){							/* Affichage index */
					$dest = "index.php";
					$this->execute_index();
				}else if($action == "login"){					/* Connexion adhérent */
					if(!isset($_SESSION['prenom']) || !isset($_SESSION['level'])){			/* Si pas déjà connecté */
						$dest = "login.php";
						$this->execute_login();
					}else{																	/* Si déjà connecté */
						$dest = "index.php";
						$this->execute_index();
					}
					
				}else if($action == "connexion"){				/* Etablissement de la connexion */
					if(!isset($_SESSION['level'])){				/* Si pas connecté */
						$array = array();
						$array = verifAdherent();
						if($array == TRUE){						/* Connexion réussie */
							$dest = "index.php";
							$_SESSION['id'] = $array['ad_id'];
							$_SESSION['prenom'] = $array['ad_prenom']; 
							$_SESSION['nom'] = $array['ad_nom'];
							$_SESSION['email'] = $array['ad_mail'];
							$_SESSION['level'] = $array['ad_level'];
							$this->execute_connexion();
						}else{									/* Echec connexion */
							$dest = "index.php";
							$this->execute_erreur();            /*retourne un erreur à l'utilisateur */
						}
					}else{										/* Si déjà connecté on renvoi à l'index */
						$dest = "index.php";
						$this->execute_index();
					}
				}
				else if($action == "logout"){					/* Déconnexion */
					session_unset();
					$dest = "index.php";
					$this->execute_logout();
				}else if($action == "inscription"){
					$dest = "index.php";
					$this->execute_inscription();
				}else if($action == "cours"){
					$dest = "index.php";
					$this->execute_cours();
				}else if($action == "liste_cours"){
					$dest = "index.php";
					$this->execute_liste_cours();
				}else if($action == "cours_inscription"){
					$dest = "index.php";
					$this->execute_inscription_cours();
				}else if($action == "inscrire_cours_adherent"){
					$dest = "index.php";
					$this->execute_inscrire_cours_adherent();
				}else if($action == "desinscrire_cours_adherent"){
					$dest = "index.php";
					$this->execute_desinscrire_cours_adherent();
				}else if($action == "informations"){
					$dest = "index.php";
					$this->execute_informations();
				}else if($action == "informationsModification"){
					$dest = "index.php";
					$this->execute_modification_informations();
				}else if($action == "planning"){
					$dest = "index.php";
					$this->execute_planning();
				}else if($action == "CoursGestion"){
					$this->execute_coursGestion();
				}else if($action == "CourGestion_modif"){
					$this->execute_coursGestion_modif();
				}else if($action == "CoursGestion_ajout"){
					$this->execute_coursGestion_ajout();
				}else if($action =="ProfesseurGestion"){
					$this->execute_professeurGestion();
				}else if($action =="ProfesseurGestion_ajouter"){
					$this->execute_professeurGestion_ajouter();
				}else if($action =="AdherentGestion"){
					$this->execute_adherentGestion();
				}
				if(!empty($dest))
					$this->destination = $dest;
			}
			
	}
	
	$controller = new UserController();
	$controller->process();

?>